package ca.swank.hq.ai;

import java.io.*;
import java.util.ArrayList;
import java.util.Random;

public class hqSimpleTalker
{
  private ArrayList allComments;
  private BufferedWriter commentWriter;
  protected static Random dice = new Random(System.currentTimeMillis());

  public hqSimpleTalker(String file)
  { try
    { allComments = new ArrayList();
    
      BufferedReader commentReader = new BufferedReader(new FileReader(file));
      String comment;
      while((comment = commentReader.readLine()) != null)
      { if(!comment.equals("")) allComments.add(comment);
      }
      commentReader.close();

      commentWriter = new BufferedWriter(new FileWriter(file, true));

    } catch(Exception e)
    { System.out.println("SimpleTalker init failure on file: " + file);
    }
  }

  public String getComment()
  { return (String)allComments.get(dice.nextInt(allComments.size()));
  }
  
  public void addComment(String comment)
  { try
    { commentWriter.write(comment);
      commentWriter.write('\n');
      commentWriter.flush();
    } catch(Exception e)
    { System.out.println("SimpleTalker file write failure");
    }
  }

}